博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
大项目之网上书城(九)——订单Demo
阅读量:4957 次
发布时间:2019-06-12

本文共 16819 字,大约阅读时间需要 56 分钟。

目录

大项目之网上书城(九)——订单Demo

主要改动

加了创建订单,为订单付款,删除订单。

1.OrderServiceImpl

代码

有添加、删除、付款、根据是否付款查找。还加了添加收货信息登记。

package cn.edu.bdu.mc.services.impls;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import java.util.UUID;import cn.edu.bdu.mc.beans.Book;import cn.edu.bdu.mc.beans.Order;import cn.edu.bdu.mc.beans.Orderitem;import cn.edu.bdu.mc.daos.BookDao;import cn.edu.bdu.mc.daos.OrderDao;import cn.edu.bdu.mc.daos.OrderitemDao;import cn.edu.bdu.mc.daos.impls.BookDaoImpl;import cn.edu.bdu.mc.daos.impls.OrderDaoImpl;import cn.edu.bdu.mc.daos.impls.OrderitemDaoImpl;import cn.edu.bdu.mc.services.OrderService;public class OrderServiceImpl implements OrderService {        private OrderDao orderDao = new OrderDaoImpl();    private OrderitemDao orderitemDao  = new OrderitemDaoImpl();    private BookDao bookDao = new BookDaoImpl();        @Override    public boolean addOrder(String order_id, int user_id, String name, String address, String phone, String[] book_ids) throws SQLException {        // TODO Auto-generated method stub        List
books = new ArrayList
(); for (String book_id : book_ids) { Book book = bookDao.findBookById(Integer.parseInt(book_id)); books.add(book); //如果数量不够了就本次购买失败 if(book.getCount()==0) { return false; } } Order order = new Order(); order.setUser_id(user_id); order.setReciver_name(name); order.setReciver_phone(phone); order.setReciver_address(address); order.setOrder_id(order_id); orderDao.addOrder(order); for (Book book : books) { Orderitem orderitem = new Orderitem(); orderitem.setBook_id(book.getBook_id()); orderitem.setOrder_id(order_id); orderitem.setCount(1); orderitem.setPrice(book.getPrice()); book.setCount(book.getCount()-1); bookDao.update(book); orderitemDao.addOrderitem(orderitem); } return true; } @Override public void AfterPay(String order_id) throws SQLException { Order order = orderDao.findOrderById(order_id); order.setPaystate(1); orderDao.update(order); } @Override public Order delById(String order_id) throws SQLException { // TODO Auto-generated method stub Order order = orderDao.delById(order_id); orderitemDao.delById(order_id); return order; } @Override public List
findNotPayByUser_id(int user_id) throws SQLException { // TODO Auto-generated method stub return orderDao.findOrderByUser_id(user_id, 0); } @Override public List
findAreadyPayByUser_id(int user_id) throws SQLException { // TODO Auto-generated method stub return orderDao.findOrderByUser_id(user_id, 1); } }

2.OrderDaoImpl

代码

目前有添加、删除、根据order_id查找和根据user_id查找

package cn.edu.bdu.mc.daos.impls;import java.sql.SQLException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.List;import javax.sql.DataSource;import org.apache.commons.dbutils.QueryRunner;import org.apache.commons.dbutils.handlers.BeanHandler;import org.apache.commons.dbutils.handlers.BeanListHandler;import cn.edu.bdu.mc.beans.Order;import cn.edu.bdu.mc.daos.OrderDao;import cn.edu.bdu.mc.utils.JDBCUtil;public class OrderDaoImpl implements OrderDao {    private DataSource dataSource = JDBCUtil.getDataSource();    private QueryRunner queryRunner = new QueryRunner(dataSource);              @Override    public void addOrder(Order order) throws SQLException {        // TODO Auto-generated method stub        String sql = "insert into `order`(user_id,order_id,reciver_name,reciver_phone,reciver_address,paystate,money,order_time) values(?,?,?,?,?,0,0,?)";        Date date = new Date();        SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");        String date1 = dateFormat.format(date);        queryRunner.update(sql,order.getUser_id(),order.getOrder_id(),order.getReciver_name(),order.getReciver_phone(),order.getReciver_address(),date1);    }        @Override    public void update(Order order) throws SQLException {        // TODO Auto-generated method stub        String sql = "update `order` set user_id = ?,reciver_name = ?,reciver_phone = ?,reciver_address = ?,paystate = ?,money = ?,order_time = ?";        queryRunner.update(sql,order.getUser_id(),order.getReciver_name(),order.getReciver_phone(),order.getReciver_address(),order.getPaystate(),order.getMoney(),order.getOrder_time());    }    @Override    public Order findOrderById(String order_id) throws SQLException {        // TODO Auto-generated method stub        String sql = "select * from `order` where order_id = ?";        return queryRunner.query(sql, new BeanHandler
(Order.class), order_id); } @Override public Order delById(String order_id) throws SQLException { Order order = findOrderById(order_id); String sql = "delete from `order` where order_id = ?"; queryRunner.update(sql, order_id); return order; } @Override public List
findOrderByUser_id(int user_id, int paystate) throws SQLException { // TODO Auto-generated method stub String sql = "select * from `order` where user_id = ? and paystate = ?"; return queryRunner.query(sql, new BeanListHandler
(Order.class), user_id, paystate); } }

3.OrderitemDaoImpl

代码

目前有添加和删除

package cn.edu.bdu.mc.daos.impls;import java.sql.SQLException;import javax.sql.DataSource;import org.apache.commons.dbutils.QueryRunner;import cn.edu.bdu.mc.beans.Orderitem;import cn.edu.bdu.mc.daos.OrderitemDao;import cn.edu.bdu.mc.utils.JDBCUtil;public class OrderitemDaoImpl implements OrderitemDao {    private DataSource dataSource = JDBCUtil.getDataSource();    private QueryRunner queryRunner = new QueryRunner(dataSource);          @Override    public void addOrderitem(Orderitem orderitem) throws SQLException {        // TODO Auto-generated method stub        String sql = "insert into orderitem values (?,?,?,?)";        queryRunner.update(sql,orderitem.getOrder_id(),orderitem.getBook_id(),orderitem.getCount(),orderitem.getPrice());    }    @Override    public void delById(String order_id) throws SQLException {        // TODO Auto-generated method stub        String sql = "delete from `order` where order_id = ?";        queryRunner.update(sql, order_id);    }}

4.orderitem_insert触发器

代码

orderitem中每增加一项,将他对应的order_id的订单的钱增加上他的书的价格乘以数量。

DROP TRIGGER IF EXISTS `orderitem_insert`;DELIMITER ;;CREATE TRIGGER `orderitem_insert` AFTER INSERT ON `orderitem` FOR EACH ROW begin    update `order` set money = money + new.price * new.count where order_id = new.order_id;end;;DELIMITER ;

5.DelOrderServlet

代码

package cn.edu.bdu.mc.servlets;import java.io.IOException;import java.sql.SQLException;import java.util.List;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import cn.edu.bdu.mc.beans.Order;import cn.edu.bdu.mc.services.OrderService;import cn.edu.bdu.mc.services.impls.OrderServiceImpl;/** * Servlet implementation class DelOrderServlet */@WebServlet("/DelOrder")public class DelOrderServlet extends HttpServlet {    private static final long serialVersionUID = 1L;           /**     * @see HttpServlet#HttpServlet()     */    public DelOrderServlet() {        super();        // TODO Auto-generated constructor stub    }    /**     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)     */    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        try {            String order_id = request.getParameter("order_id");            OrderService orderService = new OrderServiceImpl();            Order order = orderService.delById(order_id);            response.sendRedirect(request.getContextPath()+"/client/order.jsp");        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    /**     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)     */    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub        doGet(request, response);    }}

6.NewOrderServlet

代码

package cn.edu.bdu.mc.servlets;import java.io.IOException;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.UUID;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import cn.edu.bdu.mc.beans.Order;import cn.edu.bdu.mc.beans.User;import cn.edu.bdu.mc.services.OrderService;import cn.edu.bdu.mc.services.impls.OrderServiceImpl;/** * Servlet implementation class NewOrderServlet */@WebServlet("/NewOrder")public class NewOrderServlet extends HttpServlet {    private static final long serialVersionUID = 1L;           /**     * @see HttpServlet#HttpServlet()     */    public NewOrderServlet() {        super();        // TODO Auto-generated constructor stub    }    /**     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)     */    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        if(request.getSession().getAttribute("shouhuo")==null) {            response.sendRedirect(request.getContextPath()+"/client/shouhuo.jsp");        }else {            try {                String[] book_ids = request.getParameter("book_id_list").split("#");                User user = (User) request.getSession().getAttribute("user");                int user_id = user.getUser_id();                Map
shouhuo = (Map
) request.getSession().getAttribute("shouhuo"); String name = shouhuo.get("name"); String phone = shouhuo.get("phone"); String address = shouhuo.get("address"); OrderService orderService = new OrderServiceImpl(); String order_id = UUID.randomUUID().toString(); boolean create_order = orderService.addOrder(order_id, user_id, name, address, phone, book_ids); if(create_order) { String htmlCode="\n" + "" + "" + "
" + "" + "" + "
\r\n" + "

订单创建成功!请尽快付款!

查看订单    " +"
立即付款\n" + "
" + "" + ""; response.getWriter().write(htmlCode); }else { String htmlCode="\n" + "" + "" + "
" + "" + "" + "
\r\n" + "

订单创建失败!请检查订单!

查看订单    " +"
再逛逛\n" + "
" + "" + ""; response.getWriter().write(htmlCode); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); }}

7.PayForServlet

代码

package cn.edu.bdu.mc.servlets;import java.io.IOException;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import cn.edu.bdu.mc.beans.Order;import cn.edu.bdu.mc.beans.User;import cn.edu.bdu.mc.daos.OrderDao;import cn.edu.bdu.mc.daos.impls.OrderDaoImpl;import cn.edu.bdu.mc.services.OrderService;import cn.edu.bdu.mc.services.impls.OrderServiceImpl;/** * Servlet implementation class PayForServlet */@WebServlet("/PayFor")public class PayForServlet extends HttpServlet {    private static final long serialVersionUID = 1L;           /**     * @see HttpServlet#HttpServlet()     */    public PayForServlet() {        super();        // TODO Auto-generated constructor stub    }    /**     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)     */    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {                try {            String order_id = request.getParameter("order_id");            OrderService orderService = new OrderServiceImpl();            orderService.AfterPay(order_id);            String htmlCode="\n" +                     ""                    + ""                    + "
" + "" + "" + "
\r\n" + "

购买成功!

继续购买    " +"
查看订单\n" + "
" + "" + ""; response.getWriter().write(htmlCode); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); }}

8.shouhuo.jsp

代码

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>收货地址
快递员

收货信息


效果图

1513080-20190623005111949-579551822.png

9.order.jsp

代码

<%@page import="cn.edu.bdu.mc.beans.User"%><%@page import="cn.edu.bdu.mc.services.impls.OrderServiceImpl"%><%@page import="cn.edu.bdu.mc.services.OrderService"%><%@page import="cn.edu.bdu.mc.beans.Order"%><%@page import="java.util.List"%><%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>订单<%    User user = (User)request.getSession().getAttribute("user");    OrderService orderService = new OrderServiceImpl();    List
notPay = orderService.findNotPayByUser_id(user.getUser_id()); List
areadyPay = orderService.findAreadyPayByUser_id(user.getUser_id()); %>
未付款:
<% if(notPay==null){ %> 无
<% }else{ %> <% for(Order order : notPay){%> id=
<%=order.getOrder_id() %>  money=
<%=order.getMoney() %>  
付款  
删除  
<% } %> <% } %> 已付款:
<% if(areadyPay==null){ %> 无
<% }else{ %> <% for(Order order : areadyPay){%> id=
<%=order.getOrder_id() %>  money=
<%=order.getMoney() %>  
<% } %> <% } %>

效果图

1513080-20190623005252435-122441837.png

点击付款后,还删除了一个,忘截图了

1513080-20190623005333912-1600056425.png

1513080-20190623005146748-1083489705.png

总结

今天弄得不是很多,又是各种报错。再加上开始的晚,于是拖到了现在才更。

转载于:https://www.cnblogs.com/zhangA/p/11071231.html

你可能感兴趣的文章
表格的拖拽功能
查看>>
函数的形参和实参
查看>>
文字过长 用 ... 表示 CSS实现单行、多行文本溢出显示省略号
查看>>
1Caesar加密
查看>>
【TP SRM 703 div2 500】 GCDGraph
查看>>
MapReduce 重要组件——Recordreader组件 [转]
查看>>
webdriver api
查看>>
apache 实现图标缓存客户端
查看>>
揭秘:黑客必备的Kali Linux是什么,有哪些弊端?
查看>>
linux系统的远程控制方法——学神IT教育
查看>>
springboot+mybatis报错Invalid bound statement (not found)
查看>>
Linux环境下SolrCloud集群环境搭建关键步骤
查看>>
P3565 [POI2014]HOT-Hotels
查看>>
MongoDB的简单使用
查看>>
hdfs 命令使用
查看>>
prometheus配置
查看>>
【noip2004】虫食算——剪枝DFS
查看>>
java语法之final
查看>>
python 多进程和多线程的区别
查看>>
sigar
查看>>